package com.qianfeng.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.qianfeng.biz.IGoodsInfoBiz;
import com.qianfeng.biz.impl.GoodsInfoBizImpl;
import com.qianfeng.commons.WebUtils;
import com.qianfeng.entity.GoodInfo;
import com.qianfeng.entity.ShopCar;

import net.sf.json.JSONObject;

public class ShopCarServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private IGoodsInfoBiz giBiz = new GoodsInfoBizImpl();
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");
		
		ShopCar shopCar = ShopCar.getShopCar(request.getSession());
		if("add".equals(action)){
			add(request, response, shopCar);
			
		}else if("update".equals(action)){
			int count = WebUtils.StringToInt(request.getParameter("count"), -1);
			int id = WebUtils.StringToInt(request.getParameter("id"), -1);
			
			shopCar.update(id, count);
			response.sendRedirect("shopcar.jsp");
		}else if("delete".equals(action)){
			int id = WebUtils.StringToInt(request.getParameter("id"), -1);
			
			shopCar.remove(id);
			response.sendRedirect("shopcar.jsp");
		}else if("getShopCart".equals(action)){
			getcar(response, shopCar);
		}else if("updataShopCart".equals(action)){
			updatecart(request, response, shopCar);
		}else if("clear".equals(action)){
			//清空购物车
			shopCar.getList().clear();
			response.sendRedirect("success.jsp");
		}
		
	}

	private void getcar(HttpServletResponse response, ShopCar shopCar) throws IOException {
		int size=shopCar.getTypeCount();
		//创建json对象
		JSONObject jso = new JSONObject();
		jso.put("shopCar", size);
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/json;charset=utf-8");
		response.setHeader("pragma", "no-cache");
		response.setHeader("cache-control", "no-cache");
		PrintWriter out = response.getWriter();
		out.write(jso.toString());
		System.out.println(jso.toString());
		out.flush();
	}

	private void updatecart(HttpServletRequest request, HttpServletResponse response, ShopCar shopCar)
			throws IOException {
		int count = WebUtils.StringToInt(request.getParameter("count"), -1);
		int id = WebUtils.StringToInt(request.getParameter("id"), -1);
		GoodInfo goodInfo = shopCar.update(id, count);
		//JSONObject jso = new JSONObject();
		double totalPrice = shopCar.getTotalPrice();
		JSONObject jso = JSONObject.fromObject(goodInfo);
		jso.put("totalPrice", totalPrice);
		System.out.println(jso);
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/json;charset=utf-8");
		response.setHeader("pragma", "no-cache");
		response.setHeader("cache-control", "no-cache");
		PrintWriter out = response.getWriter();
		out.write(jso.toString());
		out.flush();
	}

	private void add(HttpServletRequest request, HttpServletResponse response, ShopCar shopCar) throws IOException {

		int count = WebUtils.StringToInt(request.getParameter("count"), -1);
		int id = WebUtils.StringToInt(request.getParameter("id"), -1);
		GoodInfo goodInfo = giBiz.queryGoodInfoById(id);
		goodInfo.setCount(count);
		shopCar.add(goodInfo);
		getcar(response, shopCar);
	}

}
